﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mysoft.MyCM.Web.Controllers
{
	public class MRequestParam
	{
		/// <summary>
		/// 已登录用户
		/// </summary>
		public M.SSO.AuthUser LogonUser { get; private set; }
		/// <summary>
		/// 当前请求页面地址
		/// </summary>
		public string CurrentWindowLocation { get; private set; }
		/// <summary>
		/// 当前请求的控制器
		/// </summary>
		public string Controller { get; private set; }
		/// <summary>
		/// 当前请求的动作
		/// </summary>
		public string Action { get; private set; }
		/// <summary>
		/// 请求的远程IP
		/// </summary>
		public string RequestRemoteIP { get; private set; }
		/// <summary>
		/// 选择的客户信息
		/// </summary>
		public string SelectedCustomerCode { get; private set; }
		/// <summary>
		/// 请求唯一标识
		/// </summary>
		public string RequestIndentity { get; private set; }

		/// <summary>
		/// 初始化访问参数
		/// </summary>
		/// <param name="requestContext">请求上下文</param>
		/// <param name="viewData">控制器的数据</param>
		public static MRequestParam InitializeRequestParam(System.Web.Routing.RequestContext requestContext, System.Web.Mvc.ViewDataDictionary viewData)
		{
			MRequestParam param = new MRequestParam();
			param.Controller = requestContext.HttpContext.Request.RequestContext.RouteData.Values["controller"].ToString();
			param.Action = requestContext.HttpContext.Request.RequestContext.RouteData.Values["action"].ToString();
			param.RequestIndentity = string.Format("Mysoft.CM.{0}.{1}.{2}", param.Controller, param.Action, Guid.NewGuid());
			param.CurrentWindowLocation = requestContext.HttpContext.Request.Url.AbsoluteUri;
			param.RequestRemoteIP = M.Web.Util.GetIP();
			param.LogonUser = UserController.GetLogonUser();
			param.SelectedCustomerCode = getSelectedCustomerCode(requestContext);
			viewData.Add(REQUEST_PARAM_KEY, param);
			return param;
		}

		/// <summary>
		/// 获取访问参数
		/// </summary>
		/// <param name="viewData">控制器的数据</param>
		/// <returns></returns>
		public static MRequestParam GetRequestParam(System.Web.Mvc.ViewDataDictionary viewData)
		{
			if (viewData == null)
				throw new MInvalidRequestParamException("诶，这个访问好像是非法的，还请从正确渠道进入访问哦~", M.MExceptionLevel.Stop);
			if (!viewData.ContainsKey(REQUEST_PARAM_KEY))
				throw new MInvalidRequestParamException("请求参数好像不见了，还请从首页进入重试哦~", M.MExceptionLevel.Stop);
			if (viewData[REQUEST_PARAM_KEY] == null)
				throw new MInvalidRequestParamException("没有发现请求参数，还请从首页进入重试哦~", M.MExceptionLevel.Stop);
			try
			{
				return (MRequestParam)viewData[REQUEST_PARAM_KEY];
			}
			catch
			{
				throw new MInvalidRequestParamException("请求参数好像不对，您确认打开方式正确么？", M.MExceptionLevel.Stop);
			}
		}

		/// <summary>
		/// 从RouteData中获取客户信息
		/// </summary>
		/// <param name="requestContext"></param>
		/// <returns></returns>
		private static string getSelectedCustomerCode(System.Web.Routing.RequestContext requestContext)
		{
			if (!requestContext.RouteData.Values.ContainsKey("cst")) return string.Empty;
			if (requestContext.RouteData.Values["cst"] == null) return string.Empty;
			var cst = requestContext.RouteData.Values["cst"].ToString();
			if (string.IsNullOrEmpty(cst)) return string.Empty;
			return cst;
		}

		public const string REQUEST_PARAM_KEY = "___mrequestparam";
	}
}